Skip to content

Add & delete attachments possibility#33

Open
pierrejochem wants to merge 1 commit into
ox-it:masterfrom
pierrejochem:patch-2
Open

Add & delete attachments possibility#33
pierrejochem wants to merge 1 commit into
ox-it:masterfrom
pierrejochem:patch-2

Conversation

@pierrejochem

Copy link
Copy Markdown

@alexsdutton @cchurch @chris-bradley Please have a look.

@cchurch

cchurch commented Aug 30, 2016

Copy link
Copy Markdown
Contributor

@pierrejochem Unfortunately (or maybe fortunately?) I haven't had to use this library in awhile, and don't have access to a SharePoint environment to test. However, any implementation seems better than raising NotImplementedError to me.

@pierrejochem

Copy link
Copy Markdown
Author

But I have 😄 Great work from you guys!

@pierrejochem

pierrejochem commented Aug 31, 2016

Copy link
Copy Markdown
Author

@cchurch I could contribute to the Project. If this is possible?

@pierrejochem

Copy link
Copy Markdown
Author

@cchurch I would make the merges and testing

@bash-j

bash-j commented Sep 26, 2016

Copy link
Copy Markdown

@pierrejochem I ended up writing the exact same code for the add function. When I try to add an attachment using a str object as the content I receive a vague error:
``File "", line unknown
XMLSyntaxError

@pierrejochem

Copy link
Copy Markdown
Author

Hello @bash-j

I need more Details. Could you please try first my version from here:

https://github.com/pierrejochem/python-sharepoint

@bash-j

bash-j commented Sep 27, 2016

Copy link
Copy Markdown

Hi @pierrejochem

I found the content returned by the request was empty because the server was returning a 400 error, so lxml had nothing to parse. I discovered SharePoint is expecting a base64 array. After a lot of trial and error I found the following code works with a string, a text file and binary files.

def add(self, filename=None, content=None, upload_file=None):
        if upload_file:
            if not filename:
                filename = upload_file
            with open(upload_file, "rb") as filein:
                f = filein.read()
                econtent = base64.b64encode(f).decode()
        else:
            econtent = base64.encodebytes(content.encode()).decode()
        xml = SP.AddAttachment(SP.listName(self.list_id),
                               SP.listItemID(str(self.row_id)),
                               SP.fileName(filename),
                               SP.attachment(econtent))
        response = self.opener.post_soap(LIST_WEBSERVICE,
                                         xml,
                                         soapaction='http://schemas.microsoft.com/sharepoint/soap/AddAttachment')

@pierrejochem

Copy link
Copy Markdown
Author

Sorry, yes of course:

https://msdn.microsoft.com/en-us/library/lists.lists.addattachment(v=office.12).aspx

attachment => A byte array that contains the file to attach by using base-64 encoding.

It was self-evidently for me so I have not ask for it. But you found it by yourself and that is important.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants